var pageurl = "http://localhost:8080/CSE132B/formspage.jsp?form=";
var CURRENT_YEAR = 2012;
var CURRENT_SEASON = "Fall";


function trUrlData(row,action,form){
	var str = pageurl + form + "&action="+action ;
	var fields = $(".field");
	var data = $(".output:eq("+ row +")").children();
	var name,value;

	for(i = 0; i < data.length-1 ; i++){
		name = fields[i].name;
		value = data[i].lastChild.innerHTML;
		str += "&" + name + "="+ value;
	}

	return str;
	
}
function clearFields(){
	$(".field").each(function(){
		$(this).attr("value","");
		
	});
}

function updateFields(form){
	var str =pageurl + form + "&action=update"+ "&year=" + CURRENT_YEAR + "&season=" + CURRENT_SEASON ;
	var fields = $(".field");
	
	for(i = 0; i < fields.length ; i++){
		name = fields[i].name;
		value = fields[i].value;
		str += "&" + name + "="+ value;
	}
	
	return str;
}

function setPrereq(){
	var url = "http://localhost:8080/CSE132B/query.jsp?table=CoursePrereqs&";
	var req = $("#primary_key").val();
	var prereq = $("#prereq");
	
	if(setError(req))
		return;
	
	$.get(url + req + "=" + prereq )
	
}

function setError(val){
	var nill = (val == "" || val == "undefined");
	if(nill)
		$("#queryError").html("query error!");
	return nill;
}

function query(qrystr , pkey,func){
	var input = $('select[name="'+pkey +'"] option:selected' ).text();
	
	while( qrystr.search(/(\?)/) >= 0 ){
		qrystr = qrystr.replace("?",input);
	}
	
	var url = "http://localhost:8080/CSE132B/query.jsp?qrystr=" + qrystr;	
	url += "&value=" + input + "&pkey=" + pkey + "&year=" + CURRENT_YEAR + "&season=" + CURRENT_SEASON ;
	
	$.getJSON(url, func);
}


function runQueries(classID){	
	var tableQuery = "SELECT section_id FROM Section WHERE Section.class_id='"+ classID + "'";
	query(tableQuery,'course_num',setSectionData);
	
	var unitQry = 'SELECT units FROM CourseUnits WHERE course_num=\'?\'';
	$('#input_div').append("<div id='unit_data'><p>unit range</p><br /><select class='field' name='units'></select></div>");
	query(unitQry,'course_num',unitData);

}

function queryClass(){
	var classqry = "SELECT class_id FROM Class,Course WHERE Class.course_num='?' AND Course.course_num='?'";
	query(classqry,'course_num',setClassID);
}
function setClassID(data){
	
	$('input[name="class_id"]').attr("value",data[0]);
	runQueries(data[0]);
}

function setSectionData(data){
	addElementList(data,'option','section_id')
}

function unitData(data){
	addElementList(data, 'option', 'units');
	if($("#unit_data select").children().length <= 1){
		return;
	}
}



function addElementList(data, element, target){
	$('select[name="'+target +'"]').empty();
	var tmp ="";
	for(var i = 0; i < data.length ; i++){
		tmp += "<"+element+">" + data[i]+ "</" + element + ">";
	}
	$('select[name="'+target +'"]').html(tmp);
}



function poponclick(table,value)
{
	var url = "http://localhost:8080/CSE132B/formspage.jsp?popup=true&form="+ table + "&value=" + value;
    testwindow = window.open(url, table, "location=1,status=1,scrollbars=1,width=700,height=500");
    testwindow.moveTo(0, 0);
}

//functions for display the student informations
function getStudentDetails(){
	   $.getJSON( "StudentDetails.jsp",{student_id :$("#student_id").val()}, displayStudentResult);
	 }

function displayStudentResult(data) {
	  if ( data.error) 
	  {
	      $("#first_name").html(""); 
	      $("#mid_name").html("");
	      $("#last_name").html("");
	      $("#ssn").html("");
	      $("#residence").html("");
	      alert( data.error);
	  }
	  else
	  {      
	      $("#first_name").html(data.first_name); 
	      $("#mid_name").html(data.mid_name);
	      $("#last_name").html(data.last_name);
	      $("#ssn").html(data.ssn);
	      $("#residence").html(data.residence);
	  }
	}
//functions for display the student informations
function getClassDetails(){
	   $.getJSON( "ClassDetails.jsp",{class_id :$("#class_id").val()}, displayClassResult);
	 }
function displayClassResult(data) {
	  if ( data.error) 
	  {
	      $("#title").html(""); 
	      $("#course_num").html("");
	      $("#year").html("");
	      $("#season").html("");
	      alert( data.error);
	  }
	  else
	  {      
	      $("#title").html(data.title); 
	      $("#course_num").html(data.course_num);
	      $("#year").html(data.year);
	      $("#season").html(data.season);
	  }
	}
function getDegreeDetails(){
	   $.getJSON( "DegreeDetails.jsp",{degree_name :$("#degree_name").val()}, displayDegreeResult);
	 }
function displayDegreeResult(data) {
	  if ( data.error) 
	  {
	      $("#type").html("");
	      $("#unit").html("");
	      alert( data.error);
	  }
	  else
	  {    
	      $("#type").html(data.type);
	      $("#unit").html(data.unit);
	  }
	}
function getSectionDetails(){
	   $.getJSON( "SectionDetails.jsp",{section_id :$("#section_id").val()}, displaySectionResult);
	 }
function displaySectionResult(data) {
	  if ( data.error) 
	  {
	      $("#title").html(""); 
	      $("#course_num").html("");
	      alert( data.error);
	  }
	  else
	  {      
	      $("#title").html(data.title); 
	      $("#course_num").html(data.course_num);
	  }
	}